/*
 * TaskEditMenu.java
 *
 * Created on 23.06.2008, 23:18
 *
 * Copyright (c) 2006-2008, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package Menu;

import Tasks.TaskEdit;
import images.RosterIcons;
import io.file.browse.Browser;
import io.file.browse.BrowserHiddenFS;
import io.file.browse.BrowserListener;
import javax.microedition.lcdui.Display;
import midlet.StaticData;

/**
 *
 * @author ad
 */
public class TaskEditMenu
        extends Menu
        implements BrowserListener {
    
    private int whattodo =-1;

    private TaskEdit taskedit;
    
    /** Creates a new instance of TaskEditMenu */
    public TaskEditMenu(Display display, TaskEdit taskedit, boolean dest, boolean source) {
        super("", RosterIcons.getInstance());
        this.taskedit=taskedit;
        
        if (source) {
            addItem("Select Source from Open FS", 0);
            if (StaticData.getInstance().hiddenFS.size()>0)
                addItem("Select Source from Hidden FS", 1);
        } 
        if (dest) {
            addItem("Select Dest from Open FS", 2);
            if (StaticData.getInstance().hiddenFS.size()>0)
                addItem("Select Dest from Hidden FS", 3);
        }

        addItem("Save", 4);
        
	attachDisplay(display);
    }
    
    public void eventOk(){
	MenuItem me=(MenuItem) getFocusedObject();
        
	if (me==null)  return;
        
	int index=me.index;
	switch (index) {
	    case 0: //open fs
                whattodo=0;
                new Browser(taskedit.getSource(), display, this, false);
                break;
	    case 1: //hidden fs
                whattodo=1;
                new BrowserHiddenFS(taskedit.getSource(), display, this, false);
		break;
	    case 2: //open fs
                whattodo=2;
                new Browser(taskedit.getDest(), display, this, false);
                break;
	    case 3: //hidden fs
                whattodo=3;
                new BrowserHiddenFS(taskedit.getDest(), display, this, false);
		break;
            case 4:
                destroyView();
                taskedit.cmdOk();
        }
    }

    public void BrowserFilePathNotify(String pathSelected) {
        if (pathSelected.toLowerCase().startsWith("c:/"))
            pathSelected="USB/"+pathSelected.substring(3);
        if (pathSelected.toLowerCase().startsWith("e:/"))
            pathSelected="CARD/"+pathSelected.substring(3);
        
        switch(whattodo) {
            case 0:
                taskedit.setSource(pathSelected);
                break;
            case 1:
                taskedit.setSource(pathSelected);
                break;
            case 2:
                taskedit.setDest(pathSelected);
                break;
            case 3:
                taskedit.setDest(pathSelected);
                break;
        }
	destroyView();
    }

    public void destroyView()	{
        if (display!=null) display.setCurrent(parentView);
    }
}
